﻿using System;
using System.Xml;
using System.IO;
using System.Collections;
using System.Reflection;

namespace Loong.Console
{
    /// <summary>
    /// Parser 的摘要说明。
    /// </summary>
    public class Parser
    {
        private XmlDocument xd = null;
        private ArrayList al = null;    //对xml流解析后，会把每一个关键字字符串放入这个容器中
        private bool caseSensitive = false;    //记录当前语言大小写敏感否

        internal Parser(Languages language)    //构造函数接受一个枚举变量
        {
            //
            // TOD 在此处添加构造函数逻辑
            //

            Assembly asm = Assembly.GetExecutingAssembly();
            string filename = "";
            switch (language)    //取得xml文件名
            {
                case Languages.CSHARP:
                    filename = "csharp.xml";
                    break;
                case Languages.JSHARP:
                    filename = "jsharp.xml";
                    break;
                case Languages.SQL:
                    filename = "sql.xml";
                    break;
                case Languages.VBSCRIPT:
                    filename = "vbscript.xml";
                    break;
                case Languages.MATCH:
                    filename = "match.xml";
                    break;
                default:
                    break;
            }

          //  Stream strm = asm.GetManifestResourceStream(asm.GetName().Name + "." + filename);    //取得xml流

            //Reads the contents of the embedded file.
            StreamReader reader = new StreamReader(filename, System.Text.Encoding.UTF8); //下面的代码解析xml流


            xd = new XmlDocument();
            xd.Load(reader);

            al = new ArrayList();

            XmlElement root = xd.DocumentElement;

            XmlNodeList xnl = root.SelectNodes("/definition/word");
            for (int i = 0; i < xnl.Count; i++)
            {

                al.Add(xnl[i].ChildNodes[0].Value);
            }


            this.caseSensitive = bool.Parse(root.Attributes["caseSensitive"].Value);

        }

        public bool IsKeyWord(string word)    //判断字符串是否为关键字
        {
            bool rtn = false;
            for (int i = 0; i < al.Count; i++)
            {
                if (string.Compare(word, al[i].ToString(), !caseSensitive) == 0)
                {
                    rtn = true;
                    break;
                }
            }
            return rtn;

        }
    }
}